<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="com.sand.myTag" prefix="tag"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>分配权限</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form-item">
                    <div id="test7" class="demo-tree"></div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>
    </div>
</div>
<script src="${pageContext.request.contextPath}/layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<%--<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script>--%>
<script>
    layui.use(['tree', 'util','form'], function() {
        var tree = layui.tree
            , layer = layui.layer
            , util = layui.util
            , form = layui.form
            , $ = layui.$
            , data = [{
                <c:forEach items="${list}" var="item" varStatus="struts2">
                    title:'${item.one.name}',
                    id:${item.one.rid},
                    children:[
                        <c:forEach items="${item.oneList}" var="item2" varStatus="status3">
                        {
                            title:'${item2.two.name}',
                            id:${item2.two.rid},
                            children:[
                                <c:forEach items="${item2.twoList}" var="item3" varStatus="status4">
                                {
                                    title:'${item3.three.name}',
                                    id:${item3.three.rid},
                                    children:[
                                        <c:forEach items="${item3.threeList}" var="item4" varStatus="status5">
                                        {
                                            title:'${item4.name}',
                                            id:${item4.rid},
                                            checked:${tag:dowCheck(list2, item4.rid)}
                                        },
                                        </c:forEach>
                                    ]
                                },
                                </c:forEach>
                            ]
                        },
                        </c:forEach>
                    ]
                </c:forEach>
                }];

        //开启复选框
        tree.render({
            elem: '#test7'
            ,id: 'demo1'
            , data: data
            , showCheckbox: true
        });

        //监听提交
        form.on('submit(saveBtn)', function () {
            // var str=JSON.stringify(tree.getChecked('demo1'));
            // var arr=new Array();
            // alert(str[0])
            //
            // // for(var i=0;i<str.length;i++){
            // //
            // // }
            // for(var ite in str){
            //     if(ite=="id"){
            //         arr.push(str[ite]);
            //     }else if(ite=="children"){
            //         for(var ite2 in str[ite]){
            //             if(ite2=="id"){
            //                 arr.push(str[ite][ite2]);
            //             }else if(ite2=="children"){
            //                 for(var ite3 in str[ite][ite2]){
            //                     if(ite3=="id"){
            //                         arr.push(str[ite][ite2][ite3])
            //                     }
            //                 }
            //             }
            //         }
            //     }
            // }
            // alert(arr.length)
            $.ajax({
                url:"${pageContext.request.contextPath}/role/updateRoleAndAuth/${roleId}",
                type:"post",
                data:"data="+JSON.stringify(tree.getChecked('demo1')),
                dataType:"text",
                success:(rs)=>{
                    if(rs=="OK"){
                        layer.msg("修改成功",{icon:1,time:500},function (){
                            window.location.href="${pageContext.request.contextPath}/layui/page/table_role.jsp"
                        })
                    }else{
                        layer.msg("修改失败",{icon:2,time:500})
                    }
                }
            })
        });
        return false;
    });
</script>
</body>
</html>